<template>
	<noData :showErrorTip='noData'>
		<view slot='content'>
			<view class="course-wrap">
				<view class="item" v-for='(item,index) in courseList' :key='item.courseId' @click="goCourseDetail(item.isStarted,item.courseId)">
					<view class="item-content">
						<view>
							<image :src="item.banner" class="item-img" mode="scaleToFill"></image>
						</view>
						<view class="item-right">
							<view class="item-title">{{ item.courseName}}</view>
							<view class="item-desc">{{item.brief}}</view>
							<view class="item-num" v-if='item.isStarted==0'>
								<image class="item-icon" src='../../../static/course/icon-2man.png'></image>
								<text class="count-num">{{item.count}}</text>
							</view>
							<view v-else class="item-num">开始时间：{{item.startTime}}</view>
						</view>
					</view>
					<view v-if='item.isStarted==1' class="item-tag">即将开始</view>
				</view>
			</view>
		</view>

	</noData>
</template>

<script>
	import {
		isEmpty,showToast
	} from '@/utils/index.js'
	import CourseService from '@/services/course.js';
	const courseService = new CourseService();
	export default {
		props: {
			//是否应该加载数据，懒加载逻辑使用
			shouldLoad: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				courseList: [
					
				],
				loading: false,
				noData: false
			}
		},
		watch: {
			shouldLoad(newValue, oldValue) {
				if (newValue && isEmpty(this.courseList)) {
					this.loadData();
				}
			}
		},
		methods: {
			loadData() {
				if(this.loading){
					return;
				}
				this.loading = true;
				courseService.courseList().then((res) => {
					if (res.success) {
						this.courseList = res.result;
						this.noData = this.courseList.length==0;
					} else {
						showToast(res.message);
						this.noData = true;
					}
				}).catch(e => {
					
				}).then(() => {
					this.loading = false;
					uni.stopPullDownRefresh()
				});
			},
			goCourseDetail(isStarted, courseId) {
				if (isStarted == 1) {
					uni.showToast({
						title: '课程还未开始，请过会儿再来吧',
						icon: 'none'
					})
					return;
				}
				uni.navigateTo({
					url: `/pages/course/index?courseId=${courseId}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		overflow:hidden;
	}
	.course-wrap {
		border-radius: 10upx;
		box-shadow: 0 0 10px #eaeaea;
		width: 710upx;
		margin: 0 auto;
		margin-top: 10px;
		margin-bottom: 10px;
	}
	.item:first-child {
		.item-content {
			border-top:0;
		}
	}
	.item {
		position: relative;
		padding: 0 29upx;

		&-content {
			display: flex;
			align-items: center;
			padding: 40upx 0;
			border-top:1px #dddddd solid;
		}

		&-tag {
			position: absolute;
			right: 0;
			top: 0;
			background-color: #4a96f2;
			border-bottom-left-radius: 8upx;
			color: #FFFFFF;
			font-size: 18upx;
			padding: 0 5upx;
		}

		&-img {
			width: 243upx;
			height: 167upx;
			border-radius: 8upx;
		}

		&-right {
			margin-left: 20upx;
		}

		&-title {
			font-size: 28upx;
			color: #333;
			font-weight: bold;
			line-height: 36upx;
		}

		&-desc {
			font-size: 22upx;
			color: #666;
			padding: 18upx 0 26upx;
			line-height: 34upx;
		}

		&-num {
			font-size: 20upx;
			color: #999999;
		}

		&-icon {
			width: 21upx;
			height: 20upx;
			margin-right: 4upx;

		}
		.count-num {
			margin-left: 5upx;
		}
	}
	.error {
		width: 100%;
		height: 100%;
		background-color: #f1f1f1;
		text-align: center;
		position: absolute;
		bottom: 0;
		top: 0;
	
		&-text {
			font-size: 28upx;
			color: #999;
			padding-top: 240upx;
		}
	
		&-icon {
			width: 145upx;
			margin-top: 60upx;
		}
	
		&-btn {
			width: 320upx;
			height: 80upx;
			line-height: 80upx;
			color: #fff;
			font-size: 28upx;
			background-color: #5ea1f8;
			border-radius: 8upx;
			margin: 80upx auto 0;
		}
	}
</style>
